package example.tallydemo.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import example.tallydemo.MyEntity.AccountBook;
import example.tallydemo.R;

/**
 * Created by tuzha on 2018/4/9.
 */

public class MyListAdapter extends BaseAdapter
{
    Context context;
    LayoutInflater inflater;
    List<AccountBook> accountbooks = new ArrayList<>();


    //初始化
    public MyListAdapter() {}
    public MyListAdapter(Context context)
    {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    //添加一项
    public void addBook(AccountBook book)
    {
        accountbooks.add(book);
    }

    //添加多项
    public void addBooks(List<AccountBook> books)
    {
        for (AccountBook book:books)
        {
            accountbooks.add(book);
        }
    }

    //清除所有
    public void delAll()
    {
        accountbooks.clear();
    }



    @Override
    public int getCount()
    {
        return accountbooks.size();
    }

    @Override
    public Object getItem(int position)
    {
        return accountbooks.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.fragment_accontbook_item,parent,false);
        }
        AccountBook accountBook = (AccountBook)getItem(position);
        String showmonery;
        String classify = accountBook.getClassify();
        Boolean type = accountBook.getType();
        if (type)
        {
            showmonery = "+"+String.valueOf(accountBook.getMoney());
        }
        else
        {
            showmonery = "-"+String.valueOf(accountBook.getMoney());
        }
        ((TextView)convertView.findViewById(R.id.id_type_name)).setText(classify);
        ((TextView)convertView.findViewById(R.id.id_monery)).setText(showmonery);
        return convertView;
    }
}
